package com.byq.mcpanus.controller;

import com.byq.mcpanus.model.McpDomain;
import com.byq.mcpanus.model.McpServer;
import com.byq.mcpanus.model.DomainServer;
import com.byq.mcpanus.repository.McpDomainRepository;
import com.byq.mcpanus.repository.McpServerRepository;
import com.byq.mcpanus.repository.DomainServerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/domain")
public class DomainController {
    @Autowired
    private McpDomainRepository mcpDomainRepository;
    @Autowired
    private McpServerRepository mcpServerRepository;
    @Autowired
    private DomainServerRepository domainServerRepository;

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/list")
    public ResponseEntity<List<McpDomain>> list() {
        return ResponseEntity.ok(mcpDomainRepository.findAll());
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/create")
    public ResponseEntity<McpDomain> create(@RequestBody McpDomain domain) {
        return ResponseEntity.ok(mcpDomainRepository.save(domain));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/update")
    public ResponseEntity<McpDomain> update(@RequestBody McpDomain domain) {
        if (!mcpDomainRepository.existsById(domain.getId())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(mcpDomainRepository.save(domain));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/delete")
    public ResponseEntity<Void> delete(@RequestParam String id) {
        if (!mcpDomainRepository.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        mcpDomainRepository.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    // 获取domain关联的server
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/servers")
    public ResponseEntity<List<McpServer>> getServers(@RequestParam String domainId) {
        List<DomainServer> rels = domainServerRepository.findByDomainId(domainId);
        List<Long> serverIds = rels.stream().map(ds -> Long.valueOf(ds.getServerId())).collect(Collectors.toList());
        List<McpServer> servers = mcpServerRepository.findAllById(serverIds);
        return ResponseEntity.ok(servers);
    }

    // 单个添加server到domain
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/addServer")
    public ResponseEntity<Void> addServer(@RequestParam String domainId, @RequestParam Long serverId) {
        String relId = domainId + "_" + serverId;
        if (!domainServerRepository.existsById(relId)) {
            DomainServer ds = new DomainServer(domainId, serverId.toString());
            domainServerRepository.save(ds);
        }
        return ResponseEntity.ok().build();
    }

    // 单个移除server
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/removeServer")
    public ResponseEntity<Void> removeServer(@RequestParam String domainId, @RequestParam Long serverId) {
        String relId = domainId + "_" + serverId;
        if (domainServerRepository.existsById(relId)) {
            domainServerRepository.deleteById(relId);
        }
        return ResponseEntity.ok().build();
    }
} 